package com.sky.zzh.view;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;

import com.sky.zzh.R;

public class SimpleTimeDialog {

	private Dialog dia;
	private Context context;
	private WheelView wheelHour, wheelMin;
	private TextView mBtnOk, mBtnCancel;
	private SimpleTimeResultListener wheelListener;
	private String[] items_hour, items_min;
	private String currentText = "";
	private String time;

	public SimpleTimeDialog(Context context, String time,String[] items, String[] items_min, SimpleTimeResultListener listener) {
		this.context = context;
		this.wheelListener = listener;
		this.items_hour = items;
		this.items_min = items_min;
		this.time = time;
		dia = new Dialog(context, R.style.CalendarDialog);
		dia.setContentView(R.layout.dialog_time_simple);
		dia.setCancelable(true);
		dia.setCanceledOnTouchOutside(true);
	}

	public void show() {
		dia.show();
		setDialogPosition();
		findViews();
	}

	public void dismiss() {
		if (dia != null) {
			dia.dismiss();
		}
	}

	private void findViews() {
		wheelHour = (WheelView) dia.findViewById(R.id.id_time);
		wheelMin = (WheelView) dia.findViewById(R.id.id_min);
		// wheel.addChangingListener(wheelListener);
		wheelHour.setViewAdapter(new ArrayWheelAdapter<String>(context, items_hour));
		wheelHour.setVisibleItems(7);
		wheelMin.setViewAdapter(new ArrayWheelAdapter<String>(context, items_min));
		wheelMin.setVisibleItems(7);
		String[] s = time.split(":");
		for(int i=0;i<items_hour.length;i++){
			if(s[0].equals(items_hour[i])){
				wheelHour.setCurrentItem(i);
				break;
			}
		}
		
		for(int i=0;i<items_min.length;i++){
			if(s[1].equals(items_min[i])){
				wheelMin.setCurrentItem(i);
				break;
			}
		}
		currentText = items_hour[wheelHour.getCurrentItem()] + ":" + items_min[wheelMin.getCurrentItem()];
		wheelHour.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				currentText = items_hour[newValue] + ":" + items_min[wheelMin.getCurrentItem()];
			}
		});
		wheelMin.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				currentText = items_hour[wheelHour.getCurrentItem()] + ":" + items_min[newValue];
			}
		});
		mBtnCancel = (TextView) dia.findViewById(R.id.cancel);
		mBtnOk = (TextView) dia.findViewById(R.id.ok);
		mBtnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dia.dismiss();
			}
		});

		mBtnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dia.dismiss();
				wheelListener.onSimpleResult(dia, currentText);
			}
		});
	}

	/**
	 * dialog 位置 （宽屏，底部）
	 * 
	 * @param dialog
	 */
	private void setDialogPosition() {
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = dia.getWindow().getAttributes();
		lp.width = (display.getWidth()); // 设置宽度
		lp.x = 0;
		lp.y = display.getHeight();
		dia.getWindow().setAttributes(lp);
	}

	public interface SimpleTimeResultListener {
		public void onSimpleResult(Dialog d, String date);// 2010-12-12
		// 0:上午
		// 1:下午
	}
}
